package com.ty.zls.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.ty.zls.R;
import com.ty.zls.adapter.WaterRecordAdapter;
import com.ty.zls.bean.Account;
import com.ty.zls.bean.Water;
import com.ty.zls.components.MyReceycleView;
import com.ty.zls.task.WaterRecordTask;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WaterRecordActivity extends AppCompatActivity {

    @ViewInject(R.id.water_record_topbar)
    private QMUITopBarLayout water_record_topbar;
    @ViewInject(R.id.swipeToLoadLayout)
    private SwipeToLoadLayout swipeToLoadLayout;
    @ViewInject(R.id.swipe_target)
    private MyReceycleView receycleView;
    @ViewInject(R.id.water_record_empty)
    private QMUIEmptyView water_record_empty;
    private Map<String,String> request = new HashMap<>();
    private Account account;
    @ViewInject(R.id.gl_record_account)
    private QMUIGroupListView gl_record_account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        QMUIStatusBarHelper.translucent(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water_record);
        x.view().inject(this);

        account = (Account) getIntent().getSerializableExtra("account");
        request.put("accountId",account.getId());
        initTopBar();
        initPager();
        initGroup();
    }

    private void initGroup() {
        QMUICommonListItemView id = gl_record_account.createItemView(
                ContextCompat.getDrawable(this, R.mipmap.user_id),
                "用户编号",
                account.getId(),
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE);
        QMUICommonListItemView name = gl_record_account.createItemView(
                ContextCompat.getDrawable(this, R.mipmap.user),
                "用户名",
                account.getName(),
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE);
        QMUICommonListItemView area = gl_record_account.createItemView(
                ContextCompat.getDrawable(this, R.mipmap.address),
                "用水地址",
                account.getArea(),
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE);

        int size = QMUIDisplayHelper.dp2px(this, 20);
        QMUIGroupListView.newSection(this)
                .setTitle(null)
                .setDescription(null)
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(id, null)
                .addItemView(name, null)
                .addItemView(area, null)
                .addTo(gl_record_account);

    }

    private void initTopBar() {
        water_record_topbar.setTitle("抄表记录");
        water_record_topbar.setBackgroundColor(getResources().getColor(R.color.app_color_theme_7));
        water_record_topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WaterRecordActivity.this.finish();
            }
        });
    }


    private void initPager(){
        receycleView.setEmptyView(water_record_empty);
        receycleView.setLayout(swipeToLoadLayout);

        final List<Water> waters = new ArrayList<>();
        final WaterRecordAdapter adapter = new WaterRecordAdapter(waters, this,account);
        receycleView.setLayoutManager(new LinearLayoutManager(this));
        receycleView.setAdapter(adapter);
        OnRefreshListener onRefreshListener = new OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeToLoadLayout.setRefreshing(false);
                adapter.setPageNum(1);
                adapter.setTotal(0);
                adapter.getDatas().clear();
                request.put("pageNum", String.valueOf(1));
                request.put("pageSize", String.valueOf(adapter.getPageSize()));
                WaterRecordTask task = new WaterRecordTask(water_record_empty,WaterRecordActivity.this,adapter,swipeToLoadLayout,account.getId());
                task.execute(request);
            }
        };
        swipeToLoadLayout.setOnRefreshListener(onRefreshListener);
        swipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                request.put("pageNum", String.valueOf(adapter.getPageNum()));
                request.put("pageSize", String.valueOf(adapter.getPageSize()));
                WaterRecordTask task = new WaterRecordTask(water_record_empty,WaterRecordActivity.this,adapter,swipeToLoadLayout,account.getId());
                task.execute(request);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
        autoRefresh();
    }


    public void autoRefresh() {
        swipeToLoadLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setRefreshing(true);
            }
        });
    }

}
